﻿<!DOCTYPE html>
<html>
<head>
	<title></title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="plugins/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="plugins/material-design-iconic-font-2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet"/>
    <link href="plugins/waves-0.7.5/waves.min.css" rel="stylesheet"/>
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/add.css">
</head>
<body>
<!-- 	<input type="file" id="ex"/> -->
	<div class="upload">
		<div class="upload-files">
			<header>
				<p>
					<i class="fa fa-cloud-upload" aria-hidden="true"></i>
					<span class="up">学校信息上传</span>
					<!-- <span class="load"></span> -->
				</p>
			</header>
			<div class="body" id="drop">
				<i class="fa fa-file-text-o pointer-none" aria-hidden="true"></i>
				<p class="pointer-none"><a href="../sevomafor/school.xlsx" download="学校信息.xlsx">下载模板</a><br><br><label class="body-lable" for="ex" href="#" ref="lfile" id="triggerFile">浏览</label>开始上传</p>
				<input type="file" id="ex" ref="lfile">
			</div>
			<footer>
				<div class="list-files">
					
				</div>
                <button class="listbtn">
                    点击上传
                </button>
			</footer>
		</div>
	</div>
</body>
<script src="js/jquery.min.js"></script>
<script src="plugins/bootstrap-3.3.0/js/bootstrap.min.js"></script>
<script src="plugins/waves-0.7.5/waves.min.js"></script>
<script src="js/echarts.min.js"></script>
<script src="js/xlsx.js"></script>
<script type="text/javascript">

var tlist = [];
var ajaxurl = 'http://118.24.115.242:8888/';

	$('#ex').change(function(e){
			var files = e.target.files;

            var fileReader = new FileReader();
            fileReader.onload = function(ev) {
                try {
                    var data = ev.target.result,
                        workbook = XLSX.read(data, {
                            type: 'binary'
                        }), // 以二进制流方式读取得到整份excel表格对象
                        persons = []; // 存储获取到的数据
                } catch (e) {
                    console.log('文件类型不正确');
                    $('.list-files').html("文件类型不正确");
                    $('.list-files').attr('')
                    $('.list-files').addClass("errt");
                    $('.listbtn').removeClass("listbtnmove");
                    return;
                }
				
				if (workbook.Sheets.Sheet1.A1.w != '年份' || workbook.Sheets.Sheet1.B1.w != '城市名称') {
                    $('.list-files').html("模板格式不正确,请重新下载");
                    $('.list-files').attr('')
                    $('.list-files').addClass("errt");
                    $('.listbtn').removeClass("listbtnmove");

                }else {

				    // 表格的表格范围，可用于判断表头是否数量是否正确
                    var fromTo = '';
                    // 遍历每张表读取
                    for (var sheet in workbook.Sheets) {
                        workbook.Sheets[sheet]['A1'].w = 'year';
                        workbook.Sheets[sheet]['B1'].w = 'cityName';
                        workbook.Sheets[sheet]['C1'].w = 'graduateNum';
                        workbook.Sheets[sheet]['D1'].w = 'enrollmentNum';
                        workbook.Sheets[sheet]['E1'].w = 'internalNum';
                        workbook.Sheets[sheet]['F1'].w = 'teacherNum';
                        workbook.Sheets[sheet]['G1'].w = 'majorTeacherNum';
                        workbook.Sheets[sheet]['H1'].w = 'schoolNum';
                        console.log(workbook.Sheets[sheet]);
                        if (workbook.Sheets.hasOwnProperty(sheet)) {
                            fromTo = workbook.Sheets[sheet]['!ref'];
                            console.log(fromTo);
                            persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                            // break; // 如果只取第一张表，就取消注释这行
                        }
                    }
                    
                    console.log(persons);
                    tlist = JSON.stringify(persons);
                    console.log(tlist);
                    var filename = e.currentTarget.files[0].name;
                    console.log(filename)

                    $('.list-files').html(filename);
                    $('.list-files').removeClass("errt");
                    $('.list-files').removeClass("succt");
                    $('.listbtn').addClass("listbtnmove");
                    }
                
            };
              
            fileReader.readAsBinaryString(files[0]);
		});

	$('.listbtn').click(function(){
		$.ajax({
            type: 'post',
            dataType: 'json',
            url: ajaxurl+'school/insert',
            data: {
                'data': tlist
            },
            success: function(data){
                console.log(data)
                if(true){
                    $('.list-files').html("上传文件成功");
                    $('.list-files').addClass("succt");
                    $('.listbtn').removeClass("listbtnmove");
                }
            }
        });
	});

</script>
</html>